<template>
  <div class="yrfm-config-list tag-scrollbar">
    <el-collapse v-model="type" accordion @change="handleChange">
      <el-collapse-item v-for="item in list" :key="item.listTypeId" :name="item.listTypeId">
        <template slot="title">
          <icon-svg :name="item.listTypeId | typeIcon"></icon-svg>
          {{ item.listType }} （{{ item.listTagNum }}）
        </template>
        <div class="tag-wrap" v-loading="loading">
          <yrfm-catalog
            v-if="item.listTypeId === 1"
            ref="factoryCatalog"
            @toggleLoading="toggleLoading"
            @drag="drag"
            @addItem="addItem"
            @changeClass="changeClass"
            @clearConfig="$emit('clearConfig')"
          ></yrfm-catalog>
          <yrfm-devired
            v-else-if="item.listTypeId === 7"
            :ref="'yrfmList' + item.listTypeId"
            @toggleLoading="toggleLoading"
            @drag="drag"
            @addItem="addItem"
          ></yrfm-devired>
          <yrfm-list
            :listType="item.listTypeId"
            :key="item.listTypeId"
            :ref="'yrfmList' + item.listTypeId"
            @toggleLoading="toggleLoading"
            @drag="drag"
            @addItem="addItem"
            v-else
          ></yrfm-list>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script>
import yrfmCatalog from './yrfm-catalog-list'
import yrfmDevired from './yrfm-devired-list'
import yrfmList from './yrfm-list'
export default {
  components: {
    yrfmCatalog,
    yrfmDevired,
    yrfmList
  },
  data() {
    return {
      loading: false,
      keyword: '',
      type: 1,
      list: [],
      selectClassItem: {},
      catalogClassTimer: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleChange(val) {
      if (val && val !== 1 && this.$refs['yrfmList' + val][0].getList) this.$refs['yrfmList' + val][0].getList()
    },
    getList() {
      this.loading = true
      this.$ajax({
        url: this.$ajax.setUrl('tagFactory/factoryListType'),
        method: 'get',
        params: this.$ajax.setParams()
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.list = []
          for (let i in data.data) {
            let item = Object.assign({}, data.data[i], { keyword: '', children: [] })
            this.list.push(item)
          }
          this.type = data.data[0].listTypeId
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toggleLoading(loading) {
      this.loading = loading
    },
    drag(event, item) {
      if (!item.assetId) return
      event.dataTransfer.setData('tagId', item.assetId)
      event.dataTransfer.setData('tagNm', item.assetNm)
      event.dataTransfer.setData('valueType', item.valueType)
      event.dataTransfer.setData('tagCode', item.assetCode)
      if (item.valueType === 3) event.dataTransfer.setData('operationType', '=')
      event.dataTransfer.setData('valueTypeCode', item.valueTypeCode)
      event.dataTransfer.setData('enums', JSON.stringify(item.enums))
      event.dataTransfer.setData('labTbl', item.labTbl) // 表名
      event.dataTransfer.setData('labFldMap', item.labFldMap) // 字段名
      event.dataTransfer.setData('dictTag', item.dictTag) // 取值类型中文
      event.dataTransfer.setData('dictDatabaseType', item.dbVal) // 数据库类型
      event.dataTransfer.setData('isDerLabel', item.isDerLabel)
    },
    addItem(item) {
      this.$emit('addItem', item)
    },
    changeClass(catalogClass) {
      if (catalogClass) this.selectClassItem = catalogClass
    },
    getCatalogClass() {
      return this.selectClassItem.catalogClassId
    },
    changeCatalogClass(catalogClassId) {
      if (this.selectClassItem.catalogClassId !== catalogClassId) {
        if (this.$refs.factoryCatalog && this.$refs.factoryCatalog.length) {
          this.$refs.factoryCatalog[0].selectCatalogClass = catalogClassId
          this.catalogClassTimer = null
        } else {
          this.catalogClassTimer = setTimeout(() => {
            this.changeCatalogClass(catalogClassId)
          }, 100)
        }
      }
    }
  },
  filters: {
    typeIcon(val) {
      let icon = ''
      switch (val) {
        case 1:
          // 全部
          icon = 'iconFactoryAll'
          break
        case 3:
          // 我的关注
          icon = 'iconFactoryWatch'
          break
        case 4:
          // 我的专辑
          icon = 'iconFactoryAlbum'
          break
        case 5:
          // 猜你喜欢
          icon = 'iconFactoryFav'
          break
        case 6:
          // 热门
          icon = 'iconFactoryHot'
          break
        case 7:
          // 计算型标签
          icon = 'iconCalculateTag'
          break
      }
      return icon
    }
  }
}
</script>

<style lang="scss">
.yrfm-config-list {
  width: 300px;
  height: 100%;
  border-right: 1px solid $border-color;
  overflow-y: auto;
  .el-collapse {
    height: 100%;
    color: #fff;
    background-color: #5973b2;
  }
  .el-collapse-item__header {
    font-size: 15px;
    font-weight: bold;
    padding: 0 10px 0 20px;
    color: inherit;
    background-color: #6a86cc;
    border-color: #fff;
    svg {
      width: 25px;
      height: 25px;
      margin-right: 8px;
    }
  }
  .el-collapse-item__content {
    background-color: #f0f4f7;
    padding: 10px 0;
    line-height: 1;
  }
  .el-collapse-item__wrap {
    border: 0;
  }
  .tag-wrap {
    .el-input {
      padding: 0 20px;
      margin: 5px 0 10px 0;
    }
    .el-input__inner {
      height: 35px;
      line-height: 35px;
      background-color: inherit;
      border-color: #455fbc;
    }
    .el-input__icon {
      line-height: 1 !important;
    }
    .el-input-group__append {
      padding: 0 15px;
      border-color: #455fbc;
      background-color: #455fbc;
      svg {
        width: 20px;
        height: 20px;
        color: #fff;
      }
    }
    .el-input__suffix {
      right: 20px;
    }
  }
  .yrfm-list-aside {
    color: #455fbc;
    .el-breadcrumb__inner {
      font-size: 15px;
      font-weight: bold;
      color: #455fbc !important;
      &:hover {
        color: #455fbc;
      }
    }
    .drag-item {
      padding: 0 30px 0 20px;
      line-height: 40px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      cursor: pointer;
      position: relative;
      &:hover {
        background-color: #c0d2f6;
        &:not(.tag-disabled):before {
          width: 10px;
          height: 10px;
          content: '';
          border: 2px solid rgba(#000, 0.3);
          background-color: #a8fff9;
          border-radius: 50%;
          position: absolute;
          top: 15px;
          left: 5px;
        }
        &.tag-disabled .icon-disabled {
          display: block;
        }
      }
      i {
        position: absolute;
        top: 15px;
        right: 15px;
      }
    }
    .asset-name {
      width: 100%;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .tag-item {
      padding-right: 60px;
      .list-handler {
        position: absolute;
        top: 0;
        right: 10px;
        .plus {
          display: inline-block;
          width: 18px;
          height: 18px;
          margin-top: -5px;
          border: 1px solid #5f8eec;
          color: #5f8eec;
          line-height: 15px;
          text-align: center;
          border-radius: 50%;
          vertical-align: middle;
          i {
            font-size: 12px;
            position: static;
          }
        }
        svg {
          width: 16px;
          height: 16px;
          color: #455fbc;
          margin: -5px 5px 0 10px;
          vertical-align: middle;
        }
      }
    }
    .tag-disabled {
      cursor: default;
      .asset-name {
        opacity: 0.45;
      }
      .icon-disabled {
        width: 10px;
        height: 10px;
        color: #ff0000;
        margin-top: -5px;
        position: absolute;
        top: 50%;
        left: 5px;
        display: none;
      }
    }
    p {
      padding: 0 20px;
      margin: 10px 0 20px 0;
    }
    .el-pagination {
      padding: 2px 20px;
      white-space: normal;
    }
    .el-pagination.is-background .el-pager li:not(.disabled).active {
      background-color: #455fbc;
    }
  }
}
</style>
